package com.ayuku;

import java.util.logging.Logger;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

import com.ayuku.commons.AYConst;
import com.ayuku.commons.CommonUtil;
import com.ayuku.commons.Util;
import com.ayuku.commons.business.Loginlog.LoginLogImpl;
import com.ayuku.commons.business.user.UserImpl;
import com.ayuku.retail.persistence.model.AppUsers;
/**
 * 
 * @author putu kusuma <putukn@gmail.com>
 *
 */
public class LoginForm {

	private static Logger LOG = Logger.getLogger(LoginForm.class.getName());

	Display display = new Display();
	Shell login = new Shell(display, SWT.DIALOG_TRIM);
	Label label1;
	private Label label5;
	private Label label4;
	Label label2;
	Text username;
	Text password;
	private Label label3;
	private Button button1;
	private Button button;
	Text text;
	
	public LoginForm() {
		login.setText(AYConst.APP_TITLE);
		login.setLayout(null);
		{
			label5 = new Label(login, SWT.NONE);
			label5.setBounds(44, 21, 58, 59);
			label5.setImage(SWTResourceManager.getImage(LoginForm.class, "/images/login/Padlock_h.png"));
			label5.setBackground(SWTResourceManager.getColor(215, 213, 253));
		}
		{
			label4 = new Label(login, SWT.NONE);
			label4.setBounds(32, 86, 89, 34);
			label4.setImage(SWTResourceManager.getImage(LoginForm.class, "/images/login/login.gif"));
			label4.setBackground(SWTResourceManager.getColor(215, 213, 253));
		}
		label3 = new Label(login, SWT.NONE);
		label3.setBounds(140, 5, 1, 151);
		label3.setBackground(SWTResourceManager.getColor(192, 192, 192));
		{
			label2 = new Label(login, SWT.NULL);
			label2.setBounds(160, 61, 80, 18);
			label2.setText("Password: ");
			label2.setAlignment(SWT.RIGHT);
			label2.setBackground(SWTResourceManager.getColor(215, 213, 253));
		}

		label1 = new Label(login, SWT.NULL);
		label1.setBounds(160, 30, 80, 20);
		label1.setText("User Name: ");
		label1.setAlignment(SWT.RIGHT);
		label1.setBackground(SWTResourceManager.getColor(215, 213, 253));

		username = new Text(login, SWT.SINGLE | SWT.BORDER);
		username.setBounds(242, 25, 179, 23);
		username.setTextLimit(20);
		username.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent evt) {
				usernameKeyPressed(evt);
			}
		});
		password = new Text(login, SWT.SINGLE | SWT.BORDER);
		password.setBounds(242, 56, 179, 23);
		password.setEchoChar('*');
		password.setTextLimit(20);
		password.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent evt) {
				passwordKeyPressed(evt);
			}
		});

		// System.out.println(password.getEchoChar());

		button = new Button(login, SWT.PUSH);
		button.setBounds(253, 120, 75, 28);
		button.setText("Submit");
		button.setBackground(SWTResourceManager.getColor(215, 213, 253));
		{
			button1 = new Button(login, SWT.PUSH | SWT.CENTER);
			button1.setBounds(340, 120, 71, 28);
			button1.setText("Cancel");
			button1.setBackground(SWTResourceManager.getColor(215, 213, 253));
			button1.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent evt) {
					button1WidgetSelected(evt);
				}
			});
		}
		button.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {
				doLogin();
			}
		});

		login.setBackground(SWTResourceManager.getColor(215, 213, 253));
		login.pack();
		login
				.setImage(SWTResourceManager.getImage(LoginForm.class, "/images/form/user_suit.png"));

		login.setSize(480, 200);

		// MAKING FORM CENTER
		CommonUtil.doShellCenter(display, login);
		// END


		
		login.open();

		while (!login.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}

		display.dispose();
	}

	private void button1WidgetSelected(SelectionEvent evt) {
		System.exit(0);
	}

	private void doLogin() {
		String usrName = username.getText();
		String usrPwd = password.getText();

		if (usrName == "" || usrName.trim() == "") {
			MessageBox messageBox = new MessageBox(login, SWT.OK
					| SWT.ICON_WARNING | SWT.CANCEL);
			messageBox.setMessage("Enter the User Name");
			messageBox.open();
			username.setFocus();
			return;
		}

		if (usrPwd == "" || usrPwd.trim() == "") {
			MessageBox messageBox = new MessageBox(login, SWT.OK
					| SWT.ICON_WARNING | SWT.CANCEL);
			messageBox.setMessage("Enter the Password");
			messageBox.open();
			password.setFocus();
			return;
		} else {
			try {
				AppUsers currentUser = UserImpl.doLogin(usrName, usrPwd);
				Util.setCurrentUser(currentUser);

				// System.out.println("Current User:" + currentUser);
				if (currentUser != null) {
					LoginLogImpl.add(Util.setLoginLog("login"));
					login.close();
					login.dispose();
				} else {
					Util
							.showMsgBox(
									"Login gagal,\n username atau password salah,\n silakan ulangi lagi!",
									1, login);
					Util.setCurrentUser(null);
					username.setFocus();
					return;
				}
			} catch (Exception e) {
				e.printStackTrace();
				LOG.info(e.getMessage());
				Util
						.showMsgBox(
								"Login gagal, Tidak ada respon dari server\n Kemungkinan server payroll mati atau jaringan mati atau listrik mati! ",
								2, login);

			}
		}
	}

	private void usernameKeyPressed(KeyEvent evt) {
		if (evt.keyCode == SWT.CR) {
			if (password.getText().equalsIgnoreCase("")){
				password.setFocus();	
			}else{
				doLogin();
			}
			
		}else if (evt.keyCode==SWT.ARROW_DOWN){
			password.setFocus();
		}
	}

	private void passwordKeyPressed(KeyEvent evt) {
		if (evt.keyCode == SWT.CR) {
			doLogin();
		}else if (evt.keyCode==SWT.ARROW_UP){
			username.setFocus();
		}
	}
}
